package com.zpc.myvideoserver.common.util;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : ResponseJson  //类名
 * @Description : 请求响应实体类  //描述
 * @Author : zpc20 //作者
 * @Date: 2023/10/25  23:58
 */
@Data
@ApiModel(value = "ResponseJson" ,description = "统一返回结果类")
@NoArgsConstructor
@AllArgsConstructor
public class ResponseJson<T>{

    /**
     * 返回值信息
     */
    @ApiModelProperty(value = "返回值信息")
    private String msg;
    /**
     * 返回code
     */
    @ApiModelProperty(value = "返回状态码")
    private Integer code;
    /**
     * 返回实体
     */
    @ApiModelProperty(value = "返回实体类")
    private T obj;

    /**
     * @Name: SecurityResponse
     * @Params : [response, msg]:[javax.servlet.http.HttpServletResponse, java.lang.String]
     * @Description : 方法重载,security失败时调用
     * @Author : zpc20
     * @Date : 2023/11/4 1:42
     * @Return void
     * @Throws
     */
    public static void FailResponse(HttpServletResponse response,String msg){
            SecurityResponse(response,new ResponseJson<>(msg,404,null));
    }

    /**
     * @Name: SecurityResponse
     * @Params : [response, msg]:[javax.servlet.http.HttpServletResponse, java.lang.String]
     * @Description : 方法重载,security成功时调用
     * @Author : zpc20
     * @Date : 2023/11/4 1:42
     * @Return void
     * @Throws
     */
    public static void SuccessResponse(HttpServletResponse response,String msg){
        SecurityResponse(response,new ResponseJson<>(msg,200,null));
    }

    /**
     * @Name: SecurityResponse
     * @Params : [obj]:[java.lang.Object]
     * @Description : 用于security验证统一返回方法
     * @Author : zpc20
     * @Date : 2023/11/1 22:43
     * @Return com.zpc.myvideoserver.common.util.ResponseJson
     * @Throws
     */
    public static void SecurityResponse(HttpServletResponse response,ResponseJson<Object> responseJson){
        try {
            // 设置返回数据类型
            response.setContentType("application/json;charset=UTF-8");
            // 获取输出流发送数据body
            ServletOutputStream outputStream = response.getOutputStream();
            // 写入响应返回前端数据进输出流
            outputStream.write(JSON.toJSONString(responseJson).getBytes(StandardCharsets.UTF_8));
            // 发送数据到目的地
            outputStream.flush();
            // 关闭连接
            outputStream.close();
        }catch (Exception e){
            System.out.println(e.toString());
        }
    }
    /**
     * @Name: SuccessResponse
     * @Params : [code, msg, obj]:[int, java.lang.String, java.lang.Object]
     * @Description : 成功返回
     * @Author : zpc20
     * @Date : 2023/11/1 22:40
     * @Return com.zpc.myvideoserver.common.util.ResponseJson
     * @Throws
     */
   public static <T> ResponseJson<T> SuccessResponse(String msg,T obj){

          ResponseJson<T> responseJson = new ResponseJson<>();
          responseJson.setCode(200);
          responseJson.setMsg(msg);
          responseJson.setObj(obj);
          return responseJson;
   }

    /**
     * @Name: FailResponse
     * @Params : [code, msg, obj]:[int, java.lang.String, java.lang.Object]
     * @Description : 失败返回
     * @Author : zpc20
     * @Date : 2023/11/1 22:39
     * @Return com.zpc.myvideoserver.common.util.ResponseJson
     * @Throws
     */
    public static <T> ResponseJson<T> FailResponse(String msg,T obj){

        ResponseJson<T> responseJson = new ResponseJson<>();
        responseJson.setCode(400);
        responseJson.setMsg(msg);
        responseJson.setObj(obj);
        return responseJson;
    }
}
